package com.codedancer.campus.client.api.lostFoundItem;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.codedancer.campus.client.api.lostFoundItem.ro.LostFoundItemAddOrUpdateRo;
import com.codedancer.campus.client.api.lostFoundItem.ro.LostFoundItemPageSearchRo;
import com.codedancer.campus.client.service.ILostFoundItemService;
import com.codedancer.campus.client.service.bo.lostFoundItem.LostFoundItemAddOrUpdateBo;
import com.codedancer.campus.client.service.bo.lostFoundItem.LostFoundItemPageSearchBo;
import com.codedancer.campus.client.service.vo.lostFoundItem.LostFoundItemVo;
import com.codedancer.campus.common.anno.Limit;
import com.codedancer.campus.common.anno.ViewNumber;
import com.codedancer.campus.common.core.BaseApi;
import com.codedancer.campus.common.enums.ViewNumberKeyEnum;
import com.codedancer.campus.common.response.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/lost-found-item")
@CrossOrigin(methods = {RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,
    RequestMethod.PUT}, maxAge = 3600)
@Api(value = "失物招领物品Api", description = "失物招领物品Api", tags = "失物招领物品Api")
public class LostFoundItemApi extends BaseApi {

    @Autowired
    private ILostFoundItemService lostFoundItemService;

    @PostMapping("/publish")
    @ApiOperation("发布物品")
    public ApiResponse<Void> publish(
        @RequestBody @Valid @ApiParam(value = "物品添加Ro", required = true)
        LostFoundItemAddOrUpdateRo lostFoundItemAddOrUpdateRo) {
        LostFoundItemAddOrUpdateBo lostFoundItemAddOrUpdateBo = BeanUtil.copyProperties(
            lostFoundItemAddOrUpdateRo, LostFoundItemAddOrUpdateBo.class);
        lostFoundItemAddOrUpdateBo.setCurrentUserUuid(currentLoginUuid());
        lostFoundItemService.publish(lostFoundItemAddOrUpdateBo);
        return success();
    }

    @GetMapping("/page")
    @ApiOperation("分页查询物品")
    public ApiResponse<IPage<LostFoundItemVo>> page(
        @ApiParam(value = "物品查询Ro", required = false)
        LostFoundItemPageSearchRo lostFoundItemPageSearchRo) {
        LostFoundItemPageSearchBo lostFoundItemPageSearchBo = BeanUtil.copyProperties(
            lostFoundItemPageSearchRo, LostFoundItemPageSearchBo.class);
        lostFoundItemPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(
            lostFoundItemService.page(lostFoundItemPageSearchBo));
    }

    @GetMapping("/detail/{uuid}")
    @ApiOperation("详情")
    @ViewNumber(description = "获取失物招领物品详情", key = ViewNumberKeyEnum.LOST_FOUND_ITEM)
    @Limit(key = "LOST_FOUND_ITEM_DETAIL", permitsPerSecond = 2, timeout = 500, msg = "系统繁忙，请稍后再试！")
    public ApiResponse<LostFoundItemVo> detail(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        return success(lostFoundItemService.detail(uuid));
    }

    @GetMapping("/page/user")
    @ApiOperation("分页查询用户物品")
    public ApiResponse<IPage<LostFoundItemVo>> page2User(
        @ApiParam(value = "物品查询Ro", required = false)
        LostFoundItemPageSearchRo lostFoundItemPageSearchRo) {
        LostFoundItemPageSearchBo lostFoundItemPageSearchBo = BeanUtil.copyProperties(
            lostFoundItemPageSearchRo, LostFoundItemPageSearchBo.class);
        lostFoundItemPageSearchBo.setCurrentUserUuid(currentLoginUuid());
        return success(
            lostFoundItemService.page2User(lostFoundItemPageSearchBo));
    }

    @DeleteMapping("/delete/{uuid}")
    @ApiOperation("删除")
    public ApiResponse<Void> delete(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.delete(uuid);
        return success();
    }

    @PutMapping("/confirm-claim/{uuid}")
    @ApiOperation("确认认领")
    public ApiResponse<Void> confirmClaim(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.confirmClaim(uuid);
        return success();
    }

    @PutMapping("/confirm-find/{uuid}")
    @ApiOperation("确认找到")
    public ApiResponse<Void> confirmFind(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.confirmFind(uuid);
        return success();
    }

    @PutMapping("/revoke/{uuid}")
    @ApiOperation("撤回")
    public ApiResponse<Void> revoke(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.revoke(uuid);
        return success();
    }

    @PostMapping("/reissue/{uuid}")
    @ApiOperation("重新发布")
    public ApiResponse<Void> reissue(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.reissue(uuid);
        return success();
    }

    @PutMapping("/edit/{uuid}")
    @ApiOperation("修改物品")
    public ApiResponse<Void> edit(
        @RequestBody @Valid @ApiParam(value = "物品修改Ro", required = true) LostFoundItemAddOrUpdateRo lostFoundItemAddOrUpdateRo,
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        LostFoundItemAddOrUpdateBo lostFoundItemAddOrUpdateBo = BeanUtil
            .copyProperties(lostFoundItemAddOrUpdateRo, LostFoundItemAddOrUpdateBo.class);
        lostFoundItemAddOrUpdateBo.setUuid(uuid);
        lostFoundItemService.edit(lostFoundItemAddOrUpdateBo);
        return success();
    }

    @PutMapping("/sync-liberty-square/{uuid}")
    @ApiOperation("同步自由广场")
    public ApiResponse<Void> syncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.syncLibertySquare(uuid);
        return success();
    }

    @PutMapping("/cancel-sync-liberty-square/{uuid}")
    @ApiOperation("取消同步自由广场")
    public ApiResponse<Void> cancelSyncLibertySquare(
        @PathVariable(value = "uuid", required = true) @ApiParam(value = "物品uuid", required = true) String uuid) {
        lostFoundItemService.cancelSyncLibertySquare(uuid);
        return success();
    }
}
